// pages/goods/goods.js
var app = getApp();
Page({
  data: {
    shopid: 0,
    data: [],//数据
    goods_category: 0,//分类筛选
    goods_rank: 0,//智能排序
    tabTxt: ['筛选货品', '智能排序'],//tab文案
    tab: [true, true],//用于判断是否隐藏
    disabled: false,//加载更多按钮状态
    page: 1,//当前页码
    hasMore: false,//加载更多按钮
    moreTxt: '点击加载更多',
    dataNull: true,
    tiaojianname: "",
    tiaojianpaixu: "",
    array: ['公开', '私密'],
    goods_type: [{ name: '小米' }, { name: '路由' }],//分类名称数组
    goods_ranks: [{ name: '按订单量' }, { name: 'price' }]
  },
  onReady: function () {
    //初始化数据
    var self = this;
    self.getFilter();
  },
  onLoad: function () {
    // this.setData({
    //   data: wx.getStorageSync("goods"),
    //   shopid: wx.getStorageSync("shopid")
    // })

  },
  onShow: function () {
    console.log('ggg' + wx.getStorageSync("shopid"));
    this.setData({
      shopid: wx.getStorageSync("shopid")
    })
    var that = this;
    console.log(that.data.shopid);
    wx.request({
      url: 'https://www.wenzhixinxi.com/phptest/idsearchgoods.php', //仅为示例，并非真实的接口地址
      data: {
        id: that.data.shopid
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        console.log('rrr'+res.data.jsonObj);
        that.setData({
          data: res.data.jsonObj
        })

      }
    })
  },

  // 选项卡
  filterTab: function (e) {
    var data = [true, true], index = e.currentTarget.dataset.index;
    data[index] = !this.data.tab[index];
    this.setData({
      tab: data
    })
    console.log(this.data.tab)
  },
  godetail: function (e) {
    var id = e.currentTarget.dataset.thisclass;
    console.log("hhh" + id);
    var that = this;
    wx.request({
      url: 'https://www.wenzhixinxi.com/phptest/goodsdetail.php', //仅为示例，并非真实的接口地址
      data: {
        id: id
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        console.log(res.data.jsonObj);
        that.setData({
          detail: res.data.jsonObj
        })
        wx.setStorageSync("detail", that.data.detail);
        console.log("添加成功，现在再执行查询")
        console.log("hhh" + that.data.detail)
      }
    })
wx.navigateTo({
  url: '../sell_goodsdetail/sell_goodsdetail',
})
  },

  // 获取筛选项
  getFilter: function () {
    var self = this;
    console.log(self.data)
    self.dataFormat(self.data)
    // wx.request({
    //   url: app.api.condition,
    //   data: {
    //     type: 'housetype-style-area'
    //   },
    //   header: {
    //     'Content-Type': 'application/json'
    //   },
    //   success: function (res) {
    //     self.getData();
    //     self.setData({
    //       filterList: res.data.data
    //     });
    //     console(res.data.data)
    //   },
    //   fail: function () {
    //     console.log('error!!!!!!!!!!!!!!')
    //   }
    // })
  },
  //筛选项点击操作
  fliter: function (e) {//左边为筛选
    var self = this, id = e.currentTarget.dataset.id, txt = e.currentTarget.dataset.txt, tabTxt = this.data.tabTxt;
    console.log(e.currentTarget)//txt为所选项
    console.log("cart:" + tabTxt)//tabtxt为两个选项卡所选项
    console.log(txt)
    if (txt == "筛选货品") {
      self.setData({
        tiaojianname: ""
      });
    }
    else {
      self.setData({
        tiaojianname: txt
      });
    }
    switch (e.currentTarget.dataset.index) {
      case '0':
        tabTxt[0] = txt;//将选项卡内容改为所选内容
        self.setData({
          page: 1,
          data: [],
          tab: [true, true],
          tabTxt: tabTxt,
        });
        break;
      case '1':
        tabTxt[1] = txt;
        self.setData({
          page: 1,
          data: [],
          tab: [true, true],
          tabTxt: tabTxt,
        });
        break;
    }
    //数据筛选
    self.getData();
    wx.request({
      url: 'https://www.wenzhixinxi.com/phptest/tiaojiansearchgoods.php', //仅为示例，并非真实的接口地址
      data: {
        shopid: self.data.shopid,
        tiaojian: self.data.tiaojianname
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        console.log("条件是：" + txt)
        console.log(res.data);
        self.setData({
          data: res.data.jsonObj
        })
      }
    })

  },
  fliterpaixu: function (e) {//左边为筛选
    var self = this, id = e.currentTarget.dataset.id, txt = e.currentTarget.dataset.txt, tabTxt = this.data.tabTxt;
    console.log(e.currentTarget)//txt为所选项
    console.log("cart:" + tabTxt)//tabtxt为两个选项卡所选项
    console.log(txt)
    switch (e.currentTarget.dataset.index) {
      case '0':
        tabTxt[0] = txt;//将选项卡内容改为所选内容
        self.setData({
          page: 1,
          data: [],
          tab: [true, true],
          tabTxt: tabTxt,
        });
        break;
      case '1':
        tabTxt[1] = txt;
        self.setData({
          page: 1,
          data: [],
          tab: [true, true],
          tabTxt: tabTxt,
        });
        break;
    }
    //数据筛选
    self.getData();
    wx.request({
      url: 'https://www.wenzhixinxi.com/phptest/tiaojianpaixugoods.php', //仅为示例，并非真实的接口地址
      data: {
        shopid: self.data.shopid,
        tiaojianname: self.data.tiaojianname,
        tiaojianpaixu: txt
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        console.log("当前选择的条件" + self.data.tiaojianname);
        console.log("当前txt" + txt);
        console.log(res.data.jsonObj);
        self.setData({
          data: res.data.jsonObj
        })
      }
    })

  },
  //数据处理
  dataFormat: function (d) {
    console.log(d.data + "999")
    if (d != "") {
      if (d.data) {
        var datas = this.data.data, flag = d.data.length < 5;
        this.setData({
          data: datas,
          disabled: flag ? true : false,
          moreTxt: flag ? "已加载全部数据" : "点击加载更多",
          hasMore: true,
          dataNull: true
        });

      } else {
        this.setData({
          hasMore: false,
          dataNull: false
        });
      }
    } else {
      console.log('接口异常！')
    }
    wx.hideToast();
  },
  //加载数据
  getData: function (callback) {
    var self = this;
    wx.showToast({
      title: '加载中...',
      icon: 'loading',
      duration: 10000
    });
    console.log(self.data.data[0] + "1")
    self.dataFormat(self.data)
    wx.request({
      url: app.d.ceshiUrl + '/Api/Index/getlist',//后台文件
      data: {
        page: self.data.page,//目前获取本地
        goods_category: self.data.goods_category,
        goods_rank: self.data.rank,
      },
      header: {
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        self.dataFormat(res);
      },
      fail: function () {
        console.log('error!!!!!!!!!!!!!!')
      }
    })
  },
  //加载更多
  getMore: function () {
    var self = this;
    self.data.page++;
    self.getData(function (d) {
      self.dataFormat(d)
    });
  },
  //跳转案例详情
  goToDetaile: function (event) {
    app.globalData.caseId = event.currentTarget.dataset.gid;
    wx.navigateTo({
      url: '../detaile/detaile'
    });
  },

  //搜索控件事件
  showInput: function () {
    this.setData({
      inputShowed: true
    });
  },
  hideInput: function () {
    this.setData({
      inputVal: "",
      inputShowed: false
    });
  },
  clearInput: function () {
    this.setData({
      inputVal: ""
    });
  },

  getmsg: function (e) {
    console.log("121" + e.detail.value)
    this.setData({
      tiaojian: e.detail.value
    })
    console.log(this.data.tiaojian)//
  },

  search: function () {//搜索方法
    console.log("555")
    console.log(this.data.tiaojian)
  },

  changestate: function (e) {
    var id = e.currentTarget.dataset.thisclass;
    console.log('wzh' + id);
    var that = this;
    that.setData({
      id: id
    })
    wx.request({
      url: 'https://www.wenzhixinxi.com/phptest/goodsdetail.php', //仅为示例，并非真实的接口地址
      data: {
        id: id
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        console.log(res.data.jsonObj);
        var type = res.data.jsonObj[0].pro_type;
        console.log(type);
        if (type == 0) {
          wx.showModal({
            title: '提示',
            content: '如果您设为公开，则用户将看到该商品，是否确定？',
            showCancel: true,
            success: function (res) {
              if (res.confirm) {

                wx.request({
                  url: 'https://www.wenzhixinxi.com/phptest/tochange.php', //仅为示例，并非真实的接口地址
                  data: {
                    id: that.data.id,
                    typ: 1
                  },
                  header: {
                    'content-type': 'application/json' // 默认值
                  },
                  success: function (res) {
                    console.log('实现成功')
                    wx.switchTab({
                      url: '../sell_goods/sell_goods',
                      success: function (e) {
                        console.log('ooo');
                        var page = getCurrentPages().pop();
                        if (page == undefined || page == null) return;
                        console.log('ttt');
                        page.onShow();
                      }
                    })
                  }
                })
              }
            }
          })

        }
        else {
          wx.showModal({
            title: '提示',
            content: '如果您设为私密，则用户将看不到该商品，是否确定？',
            showCancel: true,
            success: function (res) {
              if (res.confirm) {

                wx.request({
                  url: 'https://www.wenzhixinxi.com/phptest/tochange.php', //仅为示例，并非真实的接口地址
                  data: {
                    id: that.data.id,
                    typ: 0
                  },
                  header: {
                    'content-type': 'application/json' // 默认值
                  },
                  success: function (res) {
                    console.log('改变');
                    wx.switchTab({
                      url: '../sell_goods/sell_goods',
                      success: function (e) {
                        console.log('ooo');
                        var page = getCurrentPages().pop();
                        if (page == undefined || page == null) return;
                        console.log('ttt');
                        page.onShow();
                      }
                    })
                  }
                })
              }
            }
          })
        }
      }
    })

  },

  inputTyping: function (e) {
    this.setData({
      inputVal: e.detail.value
    });
  }

});